<?php
    /**
    * Logs user management
    * Note: Permission set on openidmodule, because, loginmodule does not have permission
    */

    if (!defined('EXPONENT')) exit('');
    
    $max_item=15;
    // check permission first
    $loc=null;
    $loc->mod="openidmodule"; // because, loginmodule does not have permission, so we set permission on openidmodule
    if (exponent_permissions_check("view_userprofile",$loc) ||
    exponent_permissions_check("administrate",$loc))
    {
        exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);
        $i18n = exponent_lang_loadFile('modules/loginmodule/actions/login.php');

        global $db;
        
        // today
        $time=aboid_getCurrentTime();
        $day=date("d",$time);
        $month=date("m",$time);
        $year=date("Y",$time);
        // yesterday
        $temp=mktime(0,0,0,$month, $day, $year);
        $temp=strtotime("-1 day", $temp);
        $yday=date("d",$temp);
        $ymonth=date("m",$temp);
        $yyear=date("Y",$temp);        
        
        
        // page split
        // cal how many posts
        $count_post = $db->countObjects('user');
        $pages=floor($count_post / $max_item);
        if ($count_post % $max_item != 0)
            $pages++;

        // get page
        $page=1;
        if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
        if (($page<1) || ($page>$pages)) $page=1;
        $start_pos=($page-1)*$max_item;
        // end of pagination
        
        $sql="SELECT {$db->prefix}user.* , {$db->prefix}user.id userid, 
             (SELECT count({$db->prefix}demand_praydetail.user_id) FROM {$db->prefix}demand_praydetail WHERE vday={$day} AND vmonth={$month} AND vyear={$year} AND user_id = userid) vtoday,
             (SELECT count({$db->prefix}demand_praydetail.user_id) FROM {$db->prefix}demand_praydetail WHERE vday={$yday} AND vmonth={$ymonth} AND vyear={$yyear} AND user_id = userid) vyesterday 
             FROM ag_user
             ORDER BY vtoday, vtoday+vyesterday
             LIMIT {$start_pos}, {$max_item}";
        
        $users=$db->selectObjectsBySql($sql);

        foreach ($users as $ku=>$vu)
        {
            $vu->reg_on=date("d/m/Y",$vu->created_on)."<br>".date("h:i:s",$vu->created_on);
            //
            if ($vu->warn_date > -1)
            {
                $vu->warn_title=date("d/m/Y h:m:i",$vu->warn_date);
            }
            // get all registered pray ring
            $sql="SELECT {$db->prefix}pray_ring.*
            FROM {$db->prefix}pray_register_ring
            INNER JOIN {$db->prefix}pray_ring
            ON {$db->prefix}pray_register_ring.ring_id = {$db->prefix}pray_ring.id
            WHERE {$db->prefix}pray_register_ring.user_id = {$user->id}";
            $rings=$db->selectObjectsBySql($sql);
            $vu->rings=$rings;
        }
        
        // generate warning form
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();

        $form = new form();
        $form->meta("action","warn_user");
        $form->meta("module","loginmodule");
        $form->meta("user_id","-1");
        $form->register('content','Nội dung cảnh cáo',new texteditorcontrol("",3,82));
        $form->register('submit','',new buttongroupcontrol('Hoàn tất'));
        
        
        _ab_theme_action_register_title("Quản lý hoạt động của nhân sự cầu nguyện");
        $template = $template = new template("loginmodule","_user_activity");
        $template->assign("vday",$day);
        $template->assign("vmonth",$month);
        $template->assign("vyear",$year);
        $template->assign("yday",$yday);
        $template->assign("ymonth",$ymonth);
        $template->assign("yyear",$yyear);
        
        $template->assign("users",$users);
        $template->assign("page",$page);
        $template->assign("pages",$pages);
        $template->assign("start_pos",$start_pos);
        $template->assign("form_html",$form->toHTML());
        $template->output();

    }
    else
    {
        echo exponent_http_error(403);
    }
?>
